package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程计划 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {

    @Autowired
    private TeachplanMapper teachplanMapper;

    @Autowired
    private CourseBaseMapper courseBaseMapper;

    @Autowired
    private TeachplanMediaMapper teachplanMediaMapper;

    @Override
    public List<TeachplanDto> findTeachplayTree(long courseId) {

        List<TeachplanDto> teachplanDtos = teachplanMapper.selectTreeNodes(courseId);
        return teachplanDtos;
    }

    @Override
    public void saveTeachplan(TeachplanDto teachplanDto) {
        Long id = teachplanDto.getId();
        if (id == null) {
            Integer count = getTeachplanCount(teachplanDto);
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto, teachplan);
            teachplan.setOrderby(count + 1);
            teachplanMapper.insert(teachplan);
        } else {
            Teachplan teachplan = teachplanMapper.selectById(id);
            BeanUtils.copyProperties(teachplanDto, teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Transactional
    @Override
    public void removeTeachplan(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        Long courseId = teachplan.getCourseId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        String auditStatus = courseBase.getAuditStatus();
        if (!"202002".equals(auditStatus)) {
            XueChengPlusException.cast("删除失败，课程审核状态是未提交时方可删除。");
        }
        teachplanMapper.deleteById(id);
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId, teachplan.getId());
        teachplanMediaMapper.delete(queryWrapper);
    }

    @Override
    public void moveup(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid, teachplan.getParentid());
        queryWrapper.eq(Teachplan::getCourseId, teachplan.getCourseId());
        queryWrapper.orderByDesc(Teachplan::getOrderby);
        List<Teachplan> teachplanList = teachplanMapper.selectList(queryWrapper);
        boolean flag = false;
        Integer orderby = 0;
        for (Teachplan t : teachplanList) {
            if (flag) {
                //交换orderby
                orderby = t.getOrderby();
                t.setOrderby(teachplan.getOrderby());
                teachplan.setOrderby(orderby);

                teachplanMapper.updateById(t);
                break;
            }
            if (t.getId().equals(id)) {
                flag = true;
            }
        }
        teachplanMapper.updateById(teachplan);
    }

    @Override
    public void movedown(Long id) {
        Teachplan teachplan = teachplanMapper.selectById(id);
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getParentid, teachplan.getParentid());
        queryWrapper.eq(Teachplan::getCourseId, teachplan.getCourseId());
        queryWrapper.orderByAsc(Teachplan::getOrderby);
        List<Teachplan> teachplanList = teachplanMapper.selectList(queryWrapper);
        boolean flag = false;
        Integer orderby = 0;
        for (Teachplan t : teachplanList) {
            if (flag) {
                //交换orderby
                orderby = t.getOrderby();
                t.setOrderby(teachplan.getOrderby());
                teachplan.setOrderby(orderby);

                teachplanMapper.updateById(t);
                break;
            }
            if (t.getId().equals(id)) {
                flag = true;
            }
        }
        teachplanMapper.updateById(teachplan);
    }

    /**
     * courseId 课程id
     * parentId 父课程计划id
     *
     * @return int 最新排序号
     * @description 获取最新的排序号
     * @author Mr.M
     * @date 2022/9/9 13:43
     */
    private Integer getTeachplanCount(TeachplanDto teachplanDto) {
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Teachplan::getCourseId, teachplanDto.getCourseId());
        queryWrapper.eq(Teachplan::getParentid, teachplanDto.getParentid());
        Integer count = teachplanMapper.selectCount(queryWrapper);
        return count;
    }
}
